using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{

    int id = 0;
    public float x;
    public float y;

    private float counter;
    public float shotCooldown;

	private bool Pulando = false;
	private bool Caindo = false;
	private bool animando = true;
	
	public float cont;
	
	public static int MoreuAs; 
	
	void Awake()
	{		
		this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");	
		this.transform.Translate(-6.0f,-2.3f,0.0f);
	}	

	
    void Start()
    {
        Mesh mesh = Quad.Create(1.0f, 1.0f);

		this.gameObject.tag = "Jogador";
		
        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasManager.globalMaterial;

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
		};
    }

	public void Animacao()
	{
		if(animando == true)
		{
		if(id >= 70)
		{
			id = 0;
			Start();
		}
		else
		{
			id++;
			Start();
		}
		}
		
	}

    public void Move()
    { 
        if (Input.GetKey(KeyCode.LeftArrow))
        {
			if(this.transform.position.x > -6.0f)
			{
            	transform.Translate(-0.05f, 0, 0);
			}
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
			if(this.transform.position.x < -4.0f)
			{
            	transform.Translate(0.05f, 0, 0);
			}
        }
		if(Input.GetKey(KeyCode.Space)|| Input.GetKey(KeyCode.UpArrow))
		{
			if(this.transform.position.y < -2.0f)
			{
				Pulando = true;
				animando = true;
			}
			animando = false;
			id = 29;
		}
		
    }
	
	void Update ()
    {
		Animacao();
        Move();
		if(this.transform.position.y < -2.0f)
		{
			animando = true;
		}
		if(this.transform.position.y > -2.2f)
		{
			Caindo = true;
		}
		else Caindo = false;
		
		if(Caindo)
		{		
			transform.Translate(0.0f,-0.1f,0.0f);		
		}
		
		if(Pulando)
		{
			if(this.transform.position.y < 0.0f)	
			{
				transform.Translate(0.05f,0.25f,0.0f);
			}
			else Pulando = false;
		}
	}
}
